#include "tsqlrelationaltablemodel.h"

TSqlRelationalTableModel::TSqlRelationalTableModel(QObject *parent,const QSqlDatabase &db)
    : QSqlRelationalTableModel{parent,db}
{

}

QVariant TSqlRelationalTableModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
        return QVariant();

    if (role == Qt::UserRole) {
        // 返回与 Qt::UserRole 关联的数据
        return userData[0][index];
    }
    else if (role == Qt::UserRole+1) {
        // 返回与 Qt::UserRole 关联的数据
        return userData[1][index];
    }
    else
    {
        return QSqlRelationalTableModel::data(index,role);
    }
    return QVariant();
}

bool TSqlRelationalTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if (!index.isValid())
        return false;

    if (role == Qt::UserRole) {
        // 设置与 Qt::UserRole 关联的数据
        userData[0][index] = value;
        emit dataChanged(index, index, {role});
        return true;
    }
    else if (role == Qt::UserRole+1) {
        // 设置与 Qt::UserRole 关联的数据
        userData[1][index] = value;
        emit dataChanged(index, index, {role});
        return true;
    }
    else
    {
        return QSqlRelationalTableModel::setData(index,value,role);
    }
    return false;
}

void TSqlRelationalTableModel::initUserData()
{
    userData[0].clear();
    userData[1].clear();
}
